import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.rmi.CORBA.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        START();
    }

    public static void START() throws IOException {
//        System.out.println("test");
        MongoClient client = new MongoClient("192.168.1.173",27017);
        MongoCollection<Document> target = client.getDatabase("CCBD").getCollection("监管处罚_江苏监管局_TEST");

        File[] f_all = new File("D:\\所有程序\\程存淦\\Java\\银行\\江苏监管局\\非表格").listFiles();
        int count = 0;
        for (File f:f_all) {
            List<String> list = GetFileStr(f);

            if (f.getName().equals("1308_中国保监会江苏监管局行政处罚决定书〔2016〕71号_2016-10-26.html")){
                int i = 0;
            }

            org.jsoup.nodes.Document html = Jsoup.parse(f, "GBK", "http://example.com/");
            Document document = new Document("_id",f.getName());
            if(list.size() >0){
                document.append("文号", Util.GetWenHao(html, f));
                //          document.append("作出处罚决定的日期",list.get(list.size()-1));
                document.append("作出处罚决定的日期", Util.GetDate(html));
                document.append("机关名称",Util.GetName(f.getName()));
//                GetName(f.getName());
                document.append("处罚对象", Util.GetChuFaDuiXiang(list));
                document.append("行政处罚决定", Util.GetJueding(list, html));
                document.append("案由", Util.GetAnyou(list, html));
                document.append("行政处罚依据", Util.GetBasis(list));

                //System.out.println(document);

                count++;
                target.updateOne(new BasicDBObject("_id", document.getString("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
                System.out.println(document.get("_id"));
            }

        }

        System.out.println("共计： " + count);

        System.out.println("---------------------------------");
        ToExcel.writeToExcel();
    }


    static List<String> GetFileStr(File input) throws IOException {
        //获取file的所有文字信息
        org.jsoup.nodes.Document doc = Jsoup.parse(input, "GBK", "http://example.com/");
        List<String> list = new ArrayList<>();

        Elements children = doc.body().children();
        while (children.size() <= 1){
            children = children.first().children();
        }
        for (Element child : children){
            String str = child.text().trim().replace(" ", "").replace(" ", "");
            if (str.length() > 0 && !str.equals(" "))
                list.add(str);
        }

        return list;
    }
}
